﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Medianamik.Core;
using System.Globalization;
using Medianamik.Core.Sugar;

namespace Medianamik.UI
{
    public class BaseManagementPage<T> : BasePage where T:SystemNode<T>, new()
    {
		/// <summary>
		/// Culture par défaut pour le création de nouveau Noeuds
		/// </summary>
        protected virtual CultureInfo DefaultCulture
        {
            get
            {
                return CurrentUICulture;
            }
        }

        private T _item;
        protected T Item
        {
            get
            {
                if (_item == null)
                {
                    if (IsNew)
                    {
                        _item = NodeManager.NewNode<T>(DefaultCulture, DefaultParentNodeId);
                    }
                    else
                    {
                        _item = ConfigManager.Get<T>(ItemId);
                    }
                }
                return _item;
            }
        }

        protected virtual Guid? DefaultParentNodeId
        {
            get
            {
                return null;
            }
        }

		protected NodeType ItemType
		{
			get
			{
				return Item.NodeType;
			}
		}

        private bool? _isNew;
        protected  bool IsNew
        {
            get
            {
                return _isNew
                    ?? (_isNew = (ItemId == Guid.Empty
                                || ConfigManager.Get<T>(ItemId) == null)).Value;
            }
        }

        protected  Guid ItemId
        {
            get
            {
                Guid g;
                if (StringHelper.IsGuid(Request["id"], out  g))
                {
                    return g;
                }
                return Guid.Empty;
            }
        }
    }
}
